﻿namespace Devonline.Core;

/// <summary>
/// 系统信息
/// </summary>
public abstract class SystemInformation
{
    /// <summary>
    /// 当前登陆的用户名
    /// </summary>
    public string UserName => Environment.UserName;
    /// <summary>
    /// 当前机器名
    /// </summary>
    public string MachineName => Environment.MachineName;
    /// <summary>
    /// 获取操作系统版本号
    /// </summary>
    /// <returns></returns>
    public string GetOSVersion() => Environment.OSVersion.VersionString;
    /// <summary>
    /// 获取 CPU ID
    /// </summary>
    /// <returns></returns>
    public abstract string GetCPUID();
    /// <summary>
    /// 获取主板编号
    /// </summary>
    /// <returns></returns>
    public abstract string GetMainBoardID();
    /// <summary>
    /// 获取 MAC 地址
    /// </summary>
    /// <returns></returns>
    public abstract string[] GetMacAddress();
    /// <summary>
    /// 获取 IP 地址
    /// </summary>
    /// <returns></returns>
    public abstract string[] GetIPAddress();
    /// <summary>
    /// 获取硬盘编号
    /// </summary>
    /// <returns></returns>
    public abstract string[] GetDiskId();
    /// <summary>
    /// 获取 shell 命令行路径
    /// </summary>
    /// <returns></returns>
    protected abstract string? GetShellPath();

    /// <summary>
    /// 执行 shell 命令
    /// </summary>
    /// <param name="command">可执行的 shell 命令</param>
    /// <returns>执行结果输出</returns>
    public string[]? ExecuteShell(string command)
    {
        var fileName = GetShellPath();
        if (fileName is null)
        {
            return null;
        }

        var process = new System.Diagnostics.Process
        {
            StartInfo = new System.Diagnostics.ProcessStartInfo(fileName)
            {
                RedirectStandardInput = true,
                RedirectStandardOutput = true,
                UseShellExecute = false
            }
        };

        process.Start();
        process.StandardInput.WriteLine(command);
        process.StandardInput.Close();
        var output = process.StandardOutput.ReadToEnd();
        process.WaitForExit();
        process.Dispose();
        return output.Split(Environment.NewLine);
    }
    /// <summary>
    /// 获取系统信息
    /// </summary>
    /// <returns></returns>
    public static SystemInformation GetSystemInformation()
    {
        if (OperatingSystem.IsLinux())
        {
            return new LinuxInformation();
        }

        if (OperatingSystem.IsWindows())
        {
            return new WindowsInformation();
        }

        throw new NotImplementedException();
    }
}